package main.api;

import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import main.GlobalConfig;
import main.database.DatabaseMan;
import org.bson.Document;
import org.json.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class CatlogApi {
    /**
     * 作用：请求rootcatlog列表。
     * 数据格式要求：客户端的请求数据中无需任何内容。
     * 返回：{"result": 200, "description", description, data:[]}。其中data列表中就是所有的rootcatlog的名字。
     * @param request: HttpServletRequest
     */
    @PostMapping("/api/getrootcatlog")
    public ResponseEntity<?> getRootCatlog(HttpServletRequest request) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfRootCatlog);

        JSONObject jo = new JSONObject();

        if (collection.countDocuments() == 0) {
            jo.put("result", 404);
            jo.put("description", "没有任何分类找到");
            return ResponseEntity.badRequest().body(jo);
        }

        FindIterable<Document> documents = collection.find();
        MongoCursor<Document> iterator = documents.iterator();

        jo.put("result", 200);
        jo.put("description", "ok");

        List<String[]> nameList = new ArrayList<>();
        String[] rootcatlog = new String[2];
        rootcatlog[0] = "综合前100";
        rootcatlog[1] = "/zonghe";
        nameList.add(rootcatlog);
        while(iterator.hasNext()) {
            Document doc = iterator.next();
            rootcatlog = new String[2];
            rootcatlog[0] = (String) doc.get("name");
            rootcatlog[1] = (String) doc.get("uri");
            nameList.add(rootcatlog);
        }

        jo.put("data", nameList);

        return ResponseEntity.ok(jo.toString());
    }

    /**
     * 作用：请求指定的rootcatlog的catlog列表。
     * 数据格式要求：客户端的请求数据中必须包含"rootcatlog"的json内容，{"rootcatlog": name}
     * 返回：{"result": 200, "description", description, data:[]}。其中data列表中就是所有的catlog的名字。
     * @param request: HttpServletRequest
     */
    @PostMapping("/api/getcatlogs")
    public ResponseEntity<?> getCatlogs(HttpServletRequest request) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfCatlog);

        String rootCatlog = null;
        Map<String, String[]> map = request.getParameterMap();
        if (map.containsKey("rootcatlog")) {
            rootCatlog = map.get("rootcatlog")[0];
        }

        JSONObject jo = new JSONObject();

        if (rootCatlog == null || collection.countDocuments(new BasicDBObject("parent", rootCatlog)) == 0) {
            jo.put("result", 404);
            jo.put("description", "没有任何分类找到");
            return ResponseEntity.badRequest().body(jo);
        }
        else {
            FindIterable<Document> findIterable = collection.find(new BasicDBObject("parent", rootCatlog));
            MongoCursor<Document> cursor = findIterable.iterator();
            List<String> catlogList = new ArrayList<>();
            while (cursor.hasNext()) {
                Document doc = cursor.next();
                catlogList.add((String) doc.get("name"));
            }
            cursor.close();

            jo.put("result", 200);
            jo.put("description", "ok");
            jo.put("data", catlogList);

            return ResponseEntity.ok(jo.toString());
        }
    }
}
